<template>
  <div id="chart" />
</template>

<script>
let ECharts = require('echarts');

export default {
  props: {
    options: {
      type: Object,
      default: () => ({})
    },
    url: {
      type: String,
      default: null
    }
  },
  async mounted () {
    this.chart = ECharts.init(this.$el)
    this.chart.setOption(this.options)
    this.chart.on('click', (param) => {
      const url = param.data.url
      if (url) {
        this.reload(url)
      }
    })
    await this.reload(this.url)
  },
  methods: {
    async reload (url) {
      if (!this.chart) return
      try {
        const series = await this.$http.Get(url)
        this.chart.setOption(series)
      } catch (e) {
        // Do Nothing
      }
    }
  }
}
</script>

<style scoped>
#chart {
  width: 100%;
  height: 100%;
}
</style>
